/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import type { App } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory, createWebHistory, RouteRecordName } from 'vue-router'
import type { VabRouteRecordRaw } from './types'
import { authentication, base, isHashRouterMode } from '/@/config'
import { setupPermissions } from '/@/router/permissions'
import Layout from '/@vab/layouts/index.vue'

export const constantRoutes: VabRouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('/@/views/login/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/loginjk',
    name: 'LoginJK',
    component: () => import('/@/views/loginjk/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('/@/views/register/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/redirect',
    name: 'Redirect',
    component: () => import('/@/views/redirect/Redirect.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/403',
    name: '403',
    component: () => import('/@/views/error/403.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('/@/views/error/404.vue'),
    meta: {
      hidden: true,
    },
  },
]

export const asyncRoutes: VabRouteRecordRaw[] = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home-2-line',
    },
    children: [
      {
        path: 'index',
        name: 'Index',
        component: () => import('/@/views/index/index.vue'),
        meta: {
          title: '首页',
          icon: 'home-2-line',
          noClosable: true,
        },
      },
      {
        path: 'login_log',
        name: 'SystemLoginLog',
        component: () => import('/@/views/system/login_log/list.vue'),
        meta: {
          title: '登陆日志',
          icon: 'blogger-line',
          guard: ['r0'],
        },
      },
      {
        path: 'basic_table_log',
        name: 'SystemBasicTableLog',
        component: () => import('/@/views/system/basic_table_log/list.vue'),
        meta: {
          title: '操作日志',
          icon: 'archive-drawer-line',
          guard: ['r0'],
        },
      },
    ],
  },
  {
    path: '/system',
    name: 'System',
    component: Layout,
    meta: {
      title: '系统',
      icon: 'chat-settings-line',
      guard: ['super', 'admin', 'client'],
    },
    children: [
      {
        path: 'city',
        name: 'City',
        component: () => import('/@/views/sport/city/list.vue'),
        meta: {
          title: '城市管理',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin'],
        },
      },
      {
        path: 'cityRegion',
        name: 'CityRegion',
        component: () => import('/@/views/sport/city_region/list.vue'),
        meta: {
          title: '区域管理',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin'],
        },
      },
      {
        path: 'school',
        name: 'School',
        component: () => import('/@/views/sport/school/list.vue'),
        meta: {
          title: '学校管理',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin'],
        },
      },
      {
        path: 'config',
        name: 'Config',
        component: () => import('/@/views/sport/sys/config.vue'),
        meta: {
          title: '客户端配置',
          icon: 'account-pin-box-line',
          guard: ['client'],
        },
      },
      // {
      //   path: 'log',
      //   name: 'Log',
      //   component: () => import('/@/views/sport/sys/log.vue'),
      //   meta: {
      //     title: '服务端记录',
      //     icon: 'account-pin-box-line',
      //   },
      // },
      {
        path: 'account',
        name: 'SystemAccount',
        component: () => import('/@/views/system/account/list.vue'),
        meta: {
          guard: ['zsy'],
          title: '员工',
          icon: 'account-pin-box-line',
        },
      },

      {
        path: 'position',
        name: 'SystemPosition',
        component: () => import('/@/views/system/position/list.vue'),
        meta: {
          guard: ['zsy'],
          title: '职位',
          icon: 'bubble-chart-line',
        },
      },

      {
        path: '/obj',
        name: 'SystemObj',
        component: () => import('/@/views/system/obj/list.vue'),
        meta: {
          // guard: ['zsy'],
          hidden: true,
          title: '系统应用',
          dynamicNewTab: true,
        },
      },
      {
        path: '/objOri',
        name: 'SystemObjOri',
        component: () => import('/@/views/system/obj/list_ori.vue'),
        meta: {
          // guard: ['zsy'],
          hidden: true,
          title: '系统应用',
          dynamicNewTab: true,
        },
      },
      {
        path: '/obj_detail',
        name: 'SystemObjDetail',
        component: () => import('/@/views/system/obj/_detail.vue'),
        meta: {
          hidden: true,
          title: '详情页',
          icon: 'remixicon-line',
          activeMenu: '/application/obj/list',
          dynamicNewTab: true, //详情页根据id传参不同可打开多个
        },
      },
    ],
  },
  {
    path: '/zk',
    name: 'Zk',
    component: Layout,
    meta: {
      title: '中考',
      icon: 'chat-settings-line',
      guard: ['super', 'admin', 'client', 'region', 'region-qt','school'],
    },
    children: [
      {
        path: 'student',
        name: 'ZkStudent',
        component: () => import('/@/views/sport/zk/student.vue'),
        meta: {
          title: '学生档案',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region', 'school'],
        },
      },
      {
        path: 'studentInit',
        name: 'ZkStudentInit',
        component: () => import('/@/views/sport/zk/student_init.vue'),
        meta: {
          hidden:true,
          title: '学生初始化',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region', 'school'],
        },
      },
      {
        path: 'student_update',
        name: 'ZkStudentUpdate',
        component: () => import('/@/views/sport/zk/student_update.vue'),
        meta: {
          title: '修改申请记录',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin'],
        },
      },
      {
        path: 'zkExam',
        name: 'ZkExam',
        component: () => import('/@/views/sport/zk/exam.vue'),
        meta: {
          title: '中考考场',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'region'],
        },
      },
      // {
      //   path: 'group',
      //   name: 'ZkGroup',
      //   component: () => import('/@/views/sport/zk/group.vue'),
      //   meta: {
      //     title: '组别管理',
      //     icon: 'account-pin-box-line',
      //     guard: ['super', 'admin', 'client', 'region'],
      //   },
      // },
      {
        path: 'project',
        name: 'ZkProject',
        component: () => import('/@/views/sport/zk/standard/project.vue'),
        meta: {
          title: '考试项目',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'region', 'school'],
        },
      },
      {
        path: 'rule',
        name: 'ZkRule',
        component: () => import('/@/views/sport/zk/standard/score.vue'),
        meta: {
          title: '得分标准',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client'],
        },
      },
      {
        path: 'score',
        name: 'ZkScore',
        component: () => import('/@/views/sport/zk/exam_log.vue'),
        meta: {
          title: '考试日志',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region'],
        },
      },
      {
        path: 'inExam',
        name: 'InExam',
        component: () => import('/@/views/sport/zk/inExam.vue'),
        meta: {
          title: '入场记录',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region', 'region-qt'],
        },
      },
      {
        path: 'outExam',
        name: 'OutExam',
        component: () => import('/@/views/sport/zk/outExam.vue'),
        meta: {
          title: '出场记录',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region'],
        },
      },
      {
        path: 'zkDevice',
        name: 'zkDevice',
        component: () => import('/@/views/sport/zk/device.vue'),
        meta: {
          title: '设备管理',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region'],
        },
      },
      {
        path: 'zkImages',
        name: 'ZkImages',
        component: () => import('/@/views/sport/zk/images.vue'),
        meta: {
          title: '考生照片',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region'],
        },
      },
      {
        path: 'zkImagesError',
        name: 'ZkImagesError',
        component: () => import('/@/views/sport/zk/images_error.vue'),
        meta: {
          title: '问题照片',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region'],
        },
      },
      {
        path: 'zkError',
        name: 'ZkError',
        component: () => import('/@/views/sport/zk/error.vue'),
        meta: {
          title: '成绩校验',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region'],
        },
      },
      {
        path: 'cityscreen',
        name: 'ZkCityScreen',
        component: () => import('/@/views/sport/zk/cityReport.vue'),
        meta: {
          title: '现场大屏幕',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin'],
          target: '_blank',
        },
      },
      {
        path: 'examscreen',
        name: 'ZkExamScreen',
        component: () => import('/@/views/sport/zk/examReport.vue'),
        meta: {
          title: '考场大屏幕',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'region'],
          target: '_blank',
          hidden: true,
        },
      },
      {
        path: 'cityprojectscreen',
        name: 'ZkCityProjectScreen',
        component: () => import('/@/views/sport/zk/cityProjectReport.vue'),
        meta: {
          title: '报考统计',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin'],
          target: '_blank',
        },
      },
      {
        path: 'cityScorescreen',
        name: 'ZkCityScoreScreen',
        component: () => import('/@/views/sport/zk/cityReportScore.vue'),
        meta: {
          title: '成绩统计',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin'],
          target: '_blank',
        },
      },
      {
        path: 'screen',
        name: 'ZkScreen',
        component: () => import('/@/views/sport/zk/dataScreen.vue'),
        meta: {
          title: '考场大屏幕',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region', 'region-qt','school'],
          hidden: true,
        },
      },
      {
        path: 'report',
        name: 'ZkReport',
        component: () => import('/@/views/sport/zk/dataReport.vue'),
        meta: {
          title: '统计大屏幕',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'region'],
          hidden: true,
        },
      },
    ],
  },
  {
    path: '/jk',
    name: 'Jk',
    component: Layout,
    meta: {
      title: '健康',
      icon: 'chat-settings-line',
      guard: ['super', 'admin', 'client'],
    },
    children: [
      // {
      //   path: 'home',
      //   name: 'JkHome',
      //   component: () => import('/@/views/sport/jk/home.vue'),
      //   meta: {
      //     title: '考试现场',
      //     icon: 'account-pin-box-line',
      //     guard: ['super', 'admin', 'client'],
      //   },
      // },
      {
        path: 'student',
        name: 'JkStudent',
        component: () => import('/@/views/sport/jk/student.vue'),
        meta: {
          title: '学生档案',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'school'],
        },
      },
      {
        path: 'jkExam',
        name: 'JkExam',
        component: () => import('/@/views/sport/jk/exam.vue'),
        meta: {
          title: '健康考场',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'school'],
        },
      },
      {
        path: 'class',
        name: 'JkClass',
        component: () => import('/@/views/sport/jk/class.vue'),
        meta: {
          title: '班级管理',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client'],
        },
      },
      {
        path: 'weight',
        name: 'JkWeight',
        component: () => import('/@/views/sport/jk/standard/weight.vue'),
        meta: {
          title: '测试权重',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client'],
        },
      },
      {
        path: 'project',
        name: 'JkProject',
        component: () => import('/@/views/sport/jk/standard/project.vue'),
        meta: {
          title: '测试项目',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client'],
        },
      },
      {
        path: 'object',
        name: 'JkObject',
        component: () => import('/@/views/sport/jk/standard/object.vue'),
        meta: {
          title: '测试对象',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client'],
        },
      },
      {
        path: 'rule',
        name: 'JkRule',
        component: () => import('/@/views/sport/jk/standard/score.vue'),
        meta: {
          title: '积分规则',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client'],
        },
      },
      {
        path: 'score',
        name: 'JkScore',
        component: () => import('/@/views/sport/jk/exam_log.vue'),
        meta: {
          title: '考试日志',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'school'],
        },
      },
      {
        path: 'screen',
        name: 'JkScreen',
        component: () => import('/@/views/sport/jk/dataScreen.vue'),
        meta: {
          title: '考场大屏幕',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'school'],
          hidden: true,
        },
      },
      {
        path: 'report',
        name: 'JkReport',
        component: () => import('/@/views/sport/jk/dataReport.vue'),
        meta: {
          title: '统计大屏幕',
          icon: 'account-pin-box-line',
          guard: ['super', 'admin', 'client', 'school'],
          hidden: true,
        },
      },
    ],
  },
  {
    path: '/portal',
    name: 'Portal',
    component: () => import('/@/views/portal/Portal.vue'),
    meta: {
      title: '门户',
      icon: 'user-heart-line',
      target: '_blank',
      guard: ['zsy'],
    },
  },
  {
    path: '/product',
    name: 'Product',
    component: () => import('/@/views/portal/Product.vue'),
    meta: {
      title: '产品简介',
      hidden: true,
    },
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'NotFound',
    meta: {
      title: '404',
      hidden: true,
    },
  },
]

const router = createRouter({
  history: isHashRouterMode ? createWebHashHistory(base) : createWebHistory(base),
  routes: constantRoutes as RouteRecordRaw[],
})

const fatteningRoutes = (routes: VabRouteRecordRaw[]): VabRouteRecordRaw[] => {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

const addRouter = (routes: VabRouteRecordRaw[]) => {
  routes.forEach((route: VabRouteRecordRaw) => {
    if (!router.hasRoute(route.name)) router.addRoute(route as RouteRecordRaw)
    if (route.children) addRouter(route.children)
  })
}

export const resetRouter = (routes: VabRouteRecordRaw[] = constantRoutes) => {
  routes.map((route: VabRouteRecordRaw) => {
    if (route.children) route.children = fatteningRoutes(route.children)
  })
  router.getRoutes().forEach((route) => {
    if (route.name) {
      const routeName: RouteRecordName = route.name
      router.hasRoute(routeName) && router.removeRoute(routeName)
    }
  })
  addRouter(routes)
}

export const setupRouter = (app: App<Element>) => {
  if (authentication === 'intelligence') addRouter(asyncRoutes)
  setupPermissions(router)
  app.use(router)
  return router
}

export default router
